import http from '@/lib/http'

export interface UserPayload {
  id?: number
  username?: string
  password?: string
  nickname?: string
  phone?: string
  email?: string
  status?: number
  avatar?: string
  createTime?: string
}

// 获取用户列表
export function listUsers(params?: any) {
  return http.get('/sysUser', { params })
}

// 获取单个用户信息
export function getUser(id: string | number) {
  return http.get(`/sysUser/${id}`)
}

// 创建用户
export function createUser(data: UserPayload) {
  return http.post('/sysUser', data)
}

// 更新用户信息
export function updateUser(data: UserPayload) {
  return http.put('/sysUser', data)
}

// 删除用户
export function deleteUsers(idList: (number|string)[]) {
  const ids = idList.join(',');
  return http.delete(`/sysUser?idList=${ids}`)
}

// 上传用户头像
export function uploadAvatar(file: File, userId: number) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('userId', userId.toString());
  return http.post('/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取用户头像URL
export function getAvatarUrl(userId: number) {
  return `/avatar/${userId}`
}